package com.buka.user.controller;

import com.buka.role.entity.Role;
import com.buka.role.service.RoleService;
import com.buka.user.entity.User;
import com.buka.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserListServlet extends HttpServlet {

    UserService userService = new UserService();

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理字符编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //判断是否登录  登陆成功后，会将登录用户放进session中  key：loginUser value: user
        HttpSession session = req.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if (null != loginUser) {  //取到值  表示用户已经登录
            List<User> userList = userService.selectList();

            req.setAttribute("list", userList);

            //加载角色列表
            List<Role> list = new RoleService().list();
            req.setAttribute("roleList",list);//类似map集合
            //请求转发到jsp
            req.getRequestDispatcher("/user/list.jsp").forward(req, resp);

        } else {
            /*  未登录  跳转登录页(转发)  WEB-INF 是受保护的，不能被外部访问 */
            resp.sendRedirect("/user/user/login.jsp");
        }


    }
}
